package com.yanglei.demo.framework.core.handler;

import cn.hutool.http.HttpStatus;
import com.yanglei.demo.client.dto.CommonResult;
import com.yanglei.demo.framework.core.util.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Slf4j
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
//        log.warn("[commence][访问 URL({}) 时，用户({}) 权限不够]", request.getRequestURI(),
//                SecurityUtils.getLoginUserId(), e);
        CommonResult<Object> result = new CommonResult<>();
        result.setCode(HttpStatus.HTTP_FORBIDDEN);
        result.setMsg("没有权限操作");
        ServletUtils.writeJSON(response, response);
    }
}
